package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.AllStudentRegsiterRelations;
import com.xjc.business.entity.domain.TimePeriods;
import com.xjc.business.entity.po.schadmin.TimePeriodPo;
import com.xjc.business.entity.vo.schadmin.TimePeriodVo;
import com.xjc.business.service.AllStudentRegsiterRelationsService;
import com.xjc.business.service.TimePeriodsService;
import com.xjc.business.mapper.TimePeriodsMapper;
import com.xjc.business.service.WxUserService;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【time_periods(时间段表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:33
*/
@Service
public class TimePeriodsServiceImpl extends ServiceImpl<TimePeriodsMapper, TimePeriods>
    implements TimePeriodsService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    /**
     * 获取时间段
     *
     * @param schoolId 学校id
     * @return 数据
     */
    @Override
    public Result<List<TimePeriodVo>> getTimePeriodList(Integer schoolId) {
        LambdaQueryWrapper<TimePeriods> timePeriodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getSchId,schoolId);

        List<TimePeriods> timePeriodsList = list(timePeriodsLambdaQueryWrapper);

        List<TimePeriodVo> timePeriodVoList = timePeriodsList.stream().map(timePeriods -> {
            TimePeriodVo timePeriodVo = new TimePeriodVo();
            timePeriodVo.setId(timePeriods.getId());
            timePeriodVo.setSchId(timePeriods.getSchId());
            timePeriodVo.setType(timePeriods.getType());
            timePeriodVo.setTimeBegin(timePeriods.getTimeBegin());
            timePeriodVo.setTimeEnd(timePeriods.getTimeEnd());
            timePeriodVo.setCreateTime(timePeriods.getCreateTime());
            return timePeriodVo;
        }).collect(Collectors.toList());

        return Result.okWithData(timePeriodVoList);
    }

    /**
     * 添加时间段
     *
     * @param timePeriodPo 时间段信息
     * @return 反馈
     */
    @Override
    public Result<String> addTimePeriod(TimePeriodPo timePeriodPo) {

        LambdaQueryWrapper<TimePeriods> timePeriodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getSchId,timePeriodPo.getSchId());
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getType,timePeriodPo.getType());
        if(count(timePeriodsLambdaQueryWrapper) > 0){
            return Result.ok("该时间段已存在");
        }

        TimePeriods timePeriods = new TimePeriods();
        timePeriods.setSchId(timePeriodPo.getSchId());
        timePeriods.setType(timePeriodPo.getType());
        timePeriods.setTimeBegin(timePeriodPo.getTimeBegin());
        timePeriods.setTimeEnd(timePeriodPo.getTimeEnd());
        timePeriods.setCreateTime(new Date());
        timePeriods.setUpdateTime(new Date());
        save(timePeriods);
        return Result.okWithData("新增成功");
    }

    /**
     * 获取当前选宿状态
     * @param id 学生微信id
     * @return
     */
    @Override
    public Result<Boolean> getSelectDormitoryStatus(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
        Integer schId = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper).getSchId();

        LambdaQueryWrapper<TimePeriods> timePeriodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getSchId,schId);
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getType,0);

        TimePeriods timePeriods = getOne(timePeriodsLambdaQueryWrapper);

        if (timePeriods == null){
            return Result.okWithData(false);
        }

        Date timeBegin = timePeriods.getTimeBegin();
        Date timeEnd = timePeriods.getTimeEnd();
        Date currentTime = new Date();
        if (currentTime.after(timeBegin) && currentTime.before(timeEnd)){
            return Result.okWithData(true);
        }

        return Result.okWithData(false);
    }

    /**
     * 获取当前换宿状态
     *
     * @param id 学生微信id
     * @return
     */
    @Override
    public Result<Boolean> getExchangeDormitoryStatus(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
        Integer schId = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper).getSchId();

        LambdaQueryWrapper<TimePeriods> timePeriodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getSchId,schId);
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getType,1);

        TimePeriods timePeriods = getOne(timePeriodsLambdaQueryWrapper);

        if (timePeriods == null){
            return Result.okWithData(false);
        }

        Date timeBegin = timePeriods.getTimeBegin();
        Date timeEnd = timePeriods.getTimeEnd();
        Date currentTime = new Date();
        if (currentTime.after(timeBegin) && currentTime.before(timeEnd)){
            return Result.okWithData(true);
        }

        return Result.okWithData(false);
    }

    /**
     * 获取学生留宿申请状态
     * @param id 学生微信id
     * @return
     */
    @Override
    public Result<Boolean> getStayBedApplyStatus(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
        Integer schId = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper).getSchId();

        LambdaQueryWrapper<TimePeriods> timePeriodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getSchId,schId);
        timePeriodsLambdaQueryWrapper.eq(TimePeriods::getType,2);

        TimePeriods timePeriods = getOne(timePeriodsLambdaQueryWrapper);

        if (timePeriods == null){
            return Result.okWithData(false);
        }

        Date timeBegin = timePeriods.getTimeBegin();
        Date timeEnd = timePeriods.getTimeEnd();
        Date currentTime = new Date();
        if (currentTime.after(timeBegin) && currentTime.before(timeEnd)){
            return Result.okWithData(true);
        }

        return Result.okWithData(false);
    }
}